var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeIndex: 0,
    navList: [{
      name: '原创广场',
      _id: '0',
    },
    {
      name: '我的原创',
      _id: '1',
    },
    {
      name: '我的推荐',
      _id: '2',
    }],

    prompt: {
      hidden: !0,
      icon: '../../assets/images/iconfont-order-default.png',
      title: '暂无相关书籍',
      text: '没有教师推荐的数据，可以看看家长推荐的优秀书籍',
      gurl: '../../pages/home/home',
      gtype: "switchTab"
    }, 


    bookbg: app.myUlife.ulifeUrl + '/wx/book_bg_img.png',//学生作品背景图
    poetrybg: app.myUlife.ulifeUrl + '/wx/poetry_bg_img.png',//学生作品背景图
    xturl: app.myUlife.ulifeUrl + "/",
    tjimagedefault: '/testimage/timg1.jpg',//默认教师推荐图片

    //原创广场
    ycgc: {
      page: 1,
      limit: 20,
      totalpage: 0,
      datas: []
    },

    //我的原创
    wdyc: {
      page: 1,
      limit: 20,
      totalpage: 0,
      datas: { owfb: [], oyfb: [] }
    },

    //我的推荐
    wdtj: {
      page: 1,
      limit: 20,
      totalpage: 0,
      datas:[]
    },

    usid: '',
    keyword:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var usid = wx.getStorageSync('usid');
    this.setData({
      usid: usid
    })
    this.getycgc();//原创广场
    this.getwdyc(); //我的原创
    this.getwdtj();//我的推荐
   
  },

  //原创广场
  getycgc() {
    var that = this;
    var page = that.data.ycgc.page;
    var limit = that.data.ycgc.limit;
    var totalpage = that.data.ycgc.totalpage;
    var keyword = that.data.keyword;
    if (page != 1) {
      if (page > totalpage) {
        return;
      }
    }
    wx.request({
      url: app.myUlife.ulifeUrl + 'wxsyc/getycgc.html',
      data: {
        page: page,
        limit: limit,
        keyword: keyword,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var datas = res.data.list;
        var totalpage = res.data.totalpage;
        var odatas = that.data.ycgc.datas;
        for (var i = 0; i < datas.length; i++) {
          odatas.push(datas[i]);
        }
        var ycgc = that.data.ycgc;
        ycgc.datas = odatas;
        ycgc.totalpage = totalpage;
        that.setData({
          ycgc: ycgc
        })
      }
    })
  },

 //我的原创
  getwdyc() {
    var that = this;
    var usid = that.data.usid;
    var page = that.data.wdyc.page;
    var limit = that.data.wdyc.limit;
    var totalpage = that.data.wdyc.totalpage;
    var keyword = that.data.keyword;
    if (page != 1) {
      if (page > totalpage) {
        return;
      }
    }
    wx.request({
      url: app.myUlife.ulifeUrl + 'wxsyc/getycgc.html',
      data: {
        usid: usid,
        page: page,
        limit: limit,
        keyword: keyword
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var datas = res.data.list;
        var totalpage = res.data.totalpage;
        var odatas = that.data.wdyc.datas;
        var owfb = odatas.owfb;
        var oyfb = odatas.oyfb;
        
        for (var i = 0; i < datas.length; i++) {
          if (datas[i].BSTATE==0){
            owfb.push(datas[i]);
          }else{
            oyfb.push(datas[i]);
          }
        }
        var ndatas={};
        ndatas={ owfb: owfb,oyfb: oyfb}
        var wdyc = that.data.wdyc;
        wdyc.datas = ndatas;
        wdyc.totalpage = totalpage;
        that.setData({
          wdyc: wdyc
        })
      }
    })
  },
  
  //我的推荐
  getwdtj() {
    var that = this;
    var usid = that.data.usid;
    var page = that.data.wdtj.page;
    var limit = that.data.wdtj.limit;
    var totalpage = that.data.wdtj.totalpage;
    if (page != 1) {
      if (page > totalpage) {
        return;
      }
    }
    wx.request({
      url: app.myUlife.ulifeUrl + 'wxsyc/getwdtj.html',
      data: {
        usid: usid,
        page: page,
        limit: limit
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var datas = res.data.list;
        var totalpage = res.data.totalpage;
        var odatas = that.data.wdtj.datas;
        for (var i = 0; i < datas.length; i++) {
          odatas.push(datas[i]);
        }
        var wdtj = that.data.wdtj;
        wdtj.datas = odatas;
        wdtj.totalpage = totalpage;
        that.setData({
          wdtj: wdtj,
        })
      }
    })
  },




  //切换
  onTapTag(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.setData({
      activeIndex: index,
    })
  },
 



  //上拉刷新
  tolower() {
    var that = this;
    var index = that.data.activeIndex;
    //原创广场
    if (index == 0) {
      var ycgc = that.data.ycgc;
      var page = parseInt(ycgc.page) + 1;
      ycgc.page = page;
      this.setData({
        ycgc: ycgc
      })
      this.getycgc();
    } else if (index == 1) {
      var wdyc = that.data.wdyc;
      var page = parseInt(wdyc.page) + 1;
      wdyc.page = page;
      this.setData({
        wdyc: wdyc
      })
      //我的原创
      this.getwdyc();
    } else if (index == 2) {
      var wdtj = that.data.wdtj;
      var page = parseInt(that.data.wdtj.page) + 1;
      wdtj.page = page;
      this.setData({
        wdtj: wdtj
      })
      //我的推荐
      this.getwdtj();
    }
  },

 
  toxsycdetail(e) {
    var ycid = e.currentTarget.dataset.ycid;
    wx.navigateTo({
      url: '../../pages/xsycdetail/xsycdetail?ycid=' + ycid
    })
  },


  todetailYx(e) {
    var bid = e.currentTarget.dataset.bid;
    wx.navigateTo({
      url: '../../pages/bookDetail/bookDetail?bid=' + bid
    })
  },


  keyinput(e) {  
    this.setData({
      keyword: e.detail.value
    })
  },


  keywordss(e) {  
    var that = this;
    var index = that.data.activeIndex;
    //原创广场
    if (index == 0) {
      var ycgc = that.data.ycgc;
      ycgc.page = 1;
      ycgc.datas=[];
      this.setData({
        ycgc: ycgc
      })
      this.getycgc();
    } else if (index == 1) {
      var wdyc = that.data.wdyc;
      wdyc.page = 1;
      wdyc.datas = { owfb: [], oyfb: []};
      this.setData({
        wdyc: wdyc
      })
      //我的原创
      this.getwdyc();
    } else if (index == 2) {
      var wdtj = that.data.wdtj;
      wdtj.page = 1;
      wdtj.datas = [];
      this.setData({
        wdtj: wdtj
      })
      //我的推荐
      this.getwdtj();
    }
  },


  gotofbzp(e) {
    wx.navigateTo({
      url: '../../pages/fbzp/fbzp'
    })
  },


  gotobooktj(e) {
    wx.navigateTo({
      url: '../../pages/booktj/booktj'
    })
  },


  errormoretj: function (e) {
    if (e.type == "error") {
      var errorImgIndex = e.target.dataset.errorimg
      var wdtj = this.data.wdtj    　　　　　　　//将图片列表数据绑定到变量
      wdtj.datas[errorImgIndex].FM = this.data.tjimagedefault //错误图片替换为默认图片
      this.setData({
        wdtj: wdtj
      })
    }
  },

})